package com.open.pokergames.biz.views.rooms

import android.content.Intent
import android.os.Bundle
import com.open.pokergames.R
import com.open.pokergames.base.activity.LoadRecycleViewBaseActivity
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.biz.constant.Constant
import com.open.pokergames.biz.entity.RoomVo
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.conn.bean.WsConstant
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.open.pokergames.texas.view.RoomDetailActivity
import com.open.pokergames.texas.view.adapter.RoomForHomeListAdapter
import com.open.pokergames.texas.view.cards5.Room5DetailActivity
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_white_bg.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RoomsForUserJoinGroupActivity : LoadRecycleViewBaseActivity<RoomVo>(), OnSuperListener<RoomEntity> {

    var viewModel: BizViewModel? = null
    private var adapter: RoomForHomeListAdapter? = null

    var where = ""
    var title = ""
    var groupNo = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_load_recycle_view_load_tip_white_bg)
        where = intent.getStringExtra("where").toString()
        title = intent.getStringExtra("title").toString()
        viewModel = BizViewModel()

        setViewTitle(title + "房间")
        initView()
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    private fun initView() {
        initRecycleStaggeredGridView(loadingRecyclerView, 16, 2, 0)

        adapter = RoomForHomeListAdapter(this)
        adapter!!.setOnSuperListener(this)
        loadingRecyclerView.adapter = adapter
        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")
        loadingTipView.setLoadingViewBg(R.color.transparent)
        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<RoomVo>) {
        val pageReq = PageReq<RoomVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        pageReq.data = RoomVo()

        when (where) {
            Constant.Where.MAIN_ALL -> {
                viewModel!!.roomsForUserJoinGroup(pageReq)
            }
            Constant.Where.MAIN_7 -> {
                viewModel!!.page7CardsForGroupMember(pageReq)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<RoomEntity>>) {

        if ("roomsForUserJoinGroup" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常", R.drawable.data_empty)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<RoomEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.data.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.data_empty)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.data!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.data.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.data!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }


    override fun onClick(position: Int, itemData: RoomEntity?, flag: String?) {
        if (itemData == null) {
            XUtils.toast().show("数据异常")
            return
        }
        if (itemData.roomType == 0) {
            XUtils.intent().openActivity(this, RoomDetailActivity::class.java)
                .putStringExtra("roomNum", itemData.roomNum)
                .putStringExtra("roomName", itemData.roomName)
                .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                .start()
        } else if (itemData.roomType == 1) { // 5张
            XUtils.intent().openActivity(this, Room5DetailActivity::class.java)
                .putStringExtra("roomNum", itemData.roomNum)
                .putStringExtra("roomName", itemData.roomName)
                .requestCode(WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK)
                .start()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK && resultCode == WsConstant.TYPE_RM.TYPE_ROOM_RELEASE_BACK) {
            XUtils.toast().showLong("房间已经释放")
            refreshData()
        }
    }
}